import 'dart:convert';
import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:liuxlcommon/util/sp_utils.dart';
import 'package:liuxlcommon/util/string_utils.dart';
import 'package:liuxlcommon/view/lxl_widget.dart';
import 'package:liuxlcommon/view/my_box.dart';
import 'package:ladder_cash/app/app.dart';
import 'package:ladder_cash/app/com/view/common_view.dart';
import 'package:ladder_cash/app/page/index/sign_in_board_view.dart';
import 'package:ladder_cash/app/page/login/login_page.dart';
import 'package:ladder_cash/generated/assets.dart';
import 'package:ladder_cash/value/app_const.dart';

class Signinview extends StatefulWidget {
  // String goldCoinTxt = "";
  String goldCoinTxt;

  Signinview(this.goldCoinTxt);

  static Map signinData = {
    "allCoinNumber": 500,
    "signs": [
      {"name": "Monday", "coinNumber": 1, "status": 1},
      {"name": "Tuesday", "coinNumber": 2, "status": 0},
      {"name": "Wednesday", "coinNumber": 3, "status": 0},
      {"name": "Thursday", "coinNumber": 4, "status": 0},
      {"name": "Friday", "coinNumber": 5, "status": 0},
      {"name": "Saturday", "coinNumber": 6, "status": 0},
      {"name": "Sunday", "coinNumber": 7, "status": 0}
    ]
  };
  static List coupon = [
    {"dayCount": 5, "status": 0, "coinNumber": 30},
    {"dayCount": 10, "status": 0, "coinNumber": 48},
    {"dayCount": 30, "status": 0, "coinNumber": 99},
    {"dayCount": 100, "status": 0, "coinNumber": 324}
  ];

  @override
  State<StatefulWidget> createState() => _();

  static Map getSinginData() {
    var jsonstr = SpUtils().getStorage1('getSinginData', '');
    // jsonstr = "";
    if (StringUtils.isEmpty(jsonstr)) {
      return signinData;
    }
    return json.decode(jsonstr);
  }

  static void setSiginData(Map data) {
    SpUtils().setStorage('getSinginData', json.encode(data));
  }

  static List getCouponList() {
    var jsonstr = SpUtils().getStorage1('setCouponList', '');
    // jsonstr = "";
    if (StringUtils.isEmpty(jsonstr)) {
      return coupon;
    }
    return json.decode(jsonstr);
  }

  static void setCouponList(List data) {
    SpUtils().setStorage('setCouponList', json.encode(data));
  }
}

class _ extends State<Signinview> {
  Map signinData = Signinview.signinData;
  int weekDay = DateTime.now().weekday - 1;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    signinData = Signinview.getSinginData();
    return LColumn.eazy([top, calendarBox])
        .bgColor(Colors.white)
        .size(375.w, 270.w);
  }

  get signCount {
    int index = 0;
    for (var item in signinData['signs']) {
      if (item['status'] == 1) {
        index++;
      }
    }
    return index;
  }

  get top => LRow.eazy([
        LColumn.eazy([
          LRow.eazy([
            LImage.easy(Assets.molopNewGoldcoin, 20.w, 20.w),
            5.lsw,
            LText.easy(
                signinData['allCoinNumber'], 20.w, fw7, Color(0xFF333333))
          ]),
          4.lsh,
          LText.easy(widget.goldCoinTxt, 12.sp, null, Color(0xFF999999))
        ]).size(165.w, null),
        LContainer().size(1.w, 41.w).bgColor(Color(0xFFE8EBE8)),
        LRow.eazy([
          LImage.easy(Assets.molopNewRilei, 41.w, 37.w),
          15.lsw,
          LColumn.eazy([
            LText.easy("Sign in", 15.sp, fw7, Color(0xFF333333)),
            7.lsh,
            LRow.eazy([
              LText.easy("Signed in ", 12.sp, fw4, const Color(0xFF999999)),
              LText.easy(signCount, 12.sp, fw7, const Color(0xFFFA9A09)),
              LText.easy(" days", 12.sp, fw4, const Color(0xFF999999))
            ])
          ])
        ]).center().flex(1),
      ]).size(375.w, 81.w).onTapL(() {
        if (!App.isLogin()) {
          Get.to(LoginPage(), opaque: false);
          return;
        }
        Get.to(SignInBoardView());
      });

  get calendarBox => LContainer(
              child: Wrap(runSpacing: 4.w, spacing: 4.w, children: [
        for (int i = 0; i < signinData['signs'].length; i++)
          if (i < 6)
            itemv(signinData['signs'][i], i)
          else
            itemvseven(signinData['signs'][i])
      ]))
          .paddingAll(10.w)
          .radius(5.w)
          .bgColor(Color(0xFFFFE6C1))
          .size(345.w, 180.w);

  itemv(item, i) {
    var isGet = item['status'] == 1;
    return LColumn.eazy([
      LText.easy(item['name'], 12.sp, null,
          weekDay == i ? Colors.white : Color(0xFF6A4000)),
      10.lsh,
      isGet
          ? LImage.easy(Assets.molopNewVectorYes, 34.w, 34.w)
          : LText.easy("+${item['coinNumber']}", 20.sp, fw7, Color(0xFFFA9A09))
              .center()
              .size(37.w, 34.w)
              .bgImg(weekDay == i
                  ? Assets.molopNewEllipseYes
                  : Assets.molopNewEllipse22)
    ])
        .bgColor(weekDay != i ? Colors.white : Color(0xFFFA9A09))
        .radius(3.w)
        .center()
        .size(78.w, 78.w)
        .onTapL(() {
      if (!App.isLogin()) {
        Get.to(LoginPage(), opaque: false);
        return;
      }

      if (i == weekDay && item['status'] == 0) {
        item['status'] = 1;
        signinData['allCoinNumber'] =
            signinData['allCoinNumber'] + item['coinNumber'];
        Signinview.setSiginData(signinData);
        setState(() {});
        CommonView.commDialog(
            bgImg: Assets.molopNewJdcgbg,
            width: 375.w,
            height: 317.w,
            confirm: () {
              Get.back();
            });
      }
    });
  }

  itemvseven(item) {
    var isGet = item['status'] == 1;

    return LColumn.eazy([
      LText.easy('— Receive ward —', 12.sp, null,
          weekDay == 6 ? Colors.white : Color(0xFF6A4000)),
      10.lsh,
      LRow.eazy([
        isGet
            ? LImage.easy(Assets.molopNewVectorYes, 34.w, 34.w)
            : LText.easy(
                    "+${item['coinNumber']}", 20.sp, fw7, Color(0xFFFA9A09))
                .center()
                .size(37.w, 34.w)
                .bgImg(weekDay == 6
                    ? Assets.molopNewEllipseYes
                    : Assets.molopNewEllipse22),
        10.lsw,
        LText.easy(item['name'], 15.sp, null,
            weekDay == 6 ? Colors.white : Color(0xFF6A4000)),
      ])
    ])
        .bgColor(weekDay != 6 ? Colors.white : Color(0xFFFA9A09))
        .radius(3.w)
        .center()
        .size(160.w, 78.w);
  }
}

//
// List<Map<String, dynamic>> coupon = [
//   {"dayCount": 5, "status": 0, "coinNumber": 30}
// ];
//
// // Calculate and add coupon data for dayCount 10, 30, 100
// List<int> dayCounts = [10, 30, 100];
//
// for (int dayCount in dayCounts) {
// double coinNumberDouble = 30 * (1 + (dayCount - 5) * 0.1);
// int coinNumber = (coinNumberDouble * 0.9).round();
//
// coupon.add({
// "dayCount": dayCount,
// "status": 0,
// "coinNumber": coinNumber
// });
// }
